/**
 * Represents an entry in the DVD system
 * @author Adam Contois, Adam Domurad, Pat Goebel
 *
 */
public class DVDEntry {
	private String name;
	private int amount, priceInCents;
	private boolean rentalStatus;
	//Whether this entry was created in the current session
	private boolean currentSession;
	
	/**
	 * Utility function to create a DVD entry, from a parsed line
	 * TODO: Proper error handling
	 * @param line the DVD file line
	 * @return the DVDEntry object created from the parsed line
	 */
	static DVDEntry createEntryFromLine(String line){
		//System.out.println("Creating from line " + line);
		final int DVD_NAME_SIZE = 25;
		final int DVD_NUMBER_SIZE = 3;
		final int DVD_STATUS_SIZE = 1;
		final int DVD_PRICE_SIZE = 6;
		int start_index = 0;
		final int EXPECTED_SIZE = DVD_NAME_SIZE + 1 + DVD_NUMBER_SIZE + 1 +
				DVD_STATUS_SIZE + 1 + DVD_PRICE_SIZE;
		if (line.length () != EXPECTED_SIZE){
			System.out.println("Expected " + EXPECTED_SIZE + " character lines in DVD file.");
			return null;
		}
		
		//Read in DVD name (25 characters)
		String name = line.substring(start_index, start_index + DVD_NAME_SIZE);
		name = name.trim();//Remove unnecessary whitespace
		start_index += DVD_NAME_SIZE + 1;
		
		//Read in DVD amount (XXX)
		String number = line.substring(start_index, start_index + DVD_NUMBER_SIZE);
		start_index += DVD_NUMBER_SIZE + 1;
		
		//Read in DVD status (R or S)
		String status = line.substring(start_index, start_index + DVD_STATUS_SIZE);
		start_index += DVD_STATUS_SIZE + 1;
		
		//Read in price (XXX.XX)
		String price = line.substring(start_index, start_index + DVD_PRICE_SIZE);
		int priceInCents = (int)Double.parseDouble(price)*100;
		
		boolean isRental = status.equals("R");
		
		//Return DVDEntry with currentSession set to false since entry was read in from file
		return new DVDEntry(name, Integer.parseInt(number), priceInCents, isRental, false);
	}
	
	/**
	 * Create a DVD entry
	 * @param name name of movie for rent or sale
	 * @param amount amount of copies available
	 * @param priceInCents price (in cents) of the movie (rent or sale)
	 * @param rentalStatus whether the DVD is for rent (true) or for sale (false)
	 * @param currentSession whether the DVD was created in the current session (disallow operations)
	 */
	public DVDEntry(String name, int amount, int priceInCents, boolean rentalStatus, boolean currentSession){
		this.name = name;
		this.amount = amount;
		this.priceInCents = priceInCents;
		this.rentalStatus = rentalStatus;
		this.currentSession = currentSession;
		//System.out.println("Created DVD Entry with name="+name+", amount="+amount+", price="+FormatUtil.formatPrice(priceInCents)+", rentalStatus="+rentalStatus);
	}
	
	/**
	 * Returns the DVD entry's name
	 * @return the name of the associated movie
	 */
	public String getName() {
		return name;
	}
	/**
	 * Returns the DVD entry's amount available
	 * @return the amount of DVD's available
	 */
	public int getAmount() {
		return amount;
	}
	/**
	 * Returns the DVD entry's rental status
	 * @return true if a Rental DVD, false if a Sale DVD
	 */
	public boolean getRentalStatus() {
		return rentalStatus;
	}

	/**
	 * Returns the DVD entry's price in cents
	 * @return the price of the dvd in cents
	 */
	public int getPriceInCents() {
		return priceInCents;
	}
	
	/**
	 * Returns true if the DVD was created in the current session
	 * @return true if the DVD is from the current session, false if created from line
	 */
	public boolean getCurrentSession(){
		return this.currentSession;
	}

	/**
	 * Sets the amount of this entry to copies
	 * @param copies the number of copies to set to
	 */
	public void setAmount(int copies) {
		this.amount = copies;
		
	}
	
	/**
	 * Sets the rental status of the DVD entry
	 * @param rental true if DVD is for rent, false if for sale
	 */
	public void setRentalStatus(boolean rental) {
		this.rentalStatus = rental;		
	}
	
	/**
	 * Sets the price of the DVD entry
	 * @param price the price in cents
	 */
	public void setPriceInCents(int price) {
		this.priceInCents = price;		
	}
}
